﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Day
{
    /// <summary>
    /// 公司交易信息
    /// </summary>
    public class Company
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        /// <summary>
        /// 查询日期
        /// </summary>
        public DateTime Pdate { get; set; }

        /// <summary>
        /// 查询的公司信息
        /// </summary>
        public Models.Sys.Company TheCompany { get; set; }

        /// <summary>
        /// 查询日期
        /// </summary>
        public string QueryDate { get; set; }

        /// <summary>
        /// 小组交易信息
        /// </summary>
        public List<Models.Day.GroupDayChange> GroupDayChange { get; set; }

        /// <summary>
        /// 小组周总计
        /// </summary>
        public List<Models.Week.GroupWeekSum> GroupWeekSum { get; set; }

        /// <summary>
        /// 公司日交易信息
        /// </summary>
        public Models.Day.CompanyDayChange CompanyDayChange { get; set; }

        /// <summary>
        /// 公司五日均值
        /// </summary>
        public Models.Day.CompanyFiveDayAvg CompanyFiveDayAvg { get; set; }

        /// <summary>
        /// 公司月均值
        /// </summary>
        public Models.Month.CompanyAvgMonth CompanyAvgMonth { get; set; }

        /// <summary>
        /// 公司年均值
        /// </summary>
        public Models.Year.CompanyYearAvg CompanyYearAvg { get; set; }

        /// <summary>
        /// 公司周总计
        /// </summary>
        public Models.Week.CompanyWeekSum CompanyWeekSum { get; set; }

        /// <summary>
        /// 公司月总计
        /// </summary>
        public Models.Month.CompanyMonthSum CompanyMonthSum { get; set; }

        /// <summary>
        /// 公司年总计
        /// </summary>
        public Models.Year.CompanyYearSum CompanyYearSum { get; set; }

        /// <summary>
        /// 页面脚本
        /// </summary>
        public string JS { get; set; }


        #region 定义一些使用到的接口
        private Interface.Sys.ICompany ICompany = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.Day.IGroupDayChange IGroupDayChange = null;
        private Interface.Week.IGroupWeekSum IGroupWeekSum = null;
        private Interface.Day.ICompanyDayChange ICompanyDayChange = null;
        private Interface.Day.ICompanyFiveDayAvg ICompanyFiveDayAvg = null;
        private Interface.Month.ICompanyAvgMonth ICompanyAvgMonth = null;
        private Interface.Year.ICompanyYearAvg ICompanyYearAvg = null;
        private Interface.Week.ICompanyWeekSum ICompanyWeekSum = null;
        private Interface.Month.ICompanyMonthSum ICompanyMonthSum = null;
        private Interface.Year.ICompanyYearSum ICompanyYearSum = null;
        private Interface.Sys.IGroups IGroups = null;
        #endregion


        public Company(int queryUserId, int queryDate, DB.DBManager db)
        {
            InitInterface(db);

            //db = new DB.DBManager();
            string _year = queryDate.ToString().Substring(0, 4);
            string _month = queryDate.ToString().Substring(4, 2);
            string _day = queryDate.ToString().Substring(6, 2);
            this.QueryDate = _year + "-" + _month + "-" + _day;
            this.Pdate = Convert.ToDateTime(this.QueryDate);

            this.TheCompany = ICompany.GetCompany();

            Models.Sys.TradeHistory _tradeHistory = ITradeHistory.GetSettlementModel(queryDate.ToString());

            this.GroupDayChange = IGroupDayChange.GetCompanyDayChange(this.Pdate.Day, this.Pdate.Month, this.Pdate.Year);

            this.GroupWeekSum = IGroupWeekSum.GetGroupWeekSum(_tradeHistory.WPdate, _tradeHistory.YPdate);

            this.CompanyDayChange = ICompanyDayChange.GetCompanyDayChange(_tradeHistory.Id);

            this.CompanyFiveDayAvg = ICompanyFiveDayAvg.GetCompanyFiveDayAvg(_tradeHistory.Id);

            this.CompanyAvgMonth = ICompanyAvgMonth.GetCompanyAvgMonth(_tradeHistory.MPdate, _tradeHistory.YPdate);

            this.CompanyYearAvg = ICompanyYearAvg.GetCompanyAvgYear(_tradeHistory.YPdate);

            this.CompanyWeekSum = ICompanyWeekSum.GetCompanyWeekSum(_tradeHistory.YPdate, _tradeHistory.WPdate);

            this.CompanyMonthSum = ICompanyMonthSum.GetCompanyMonthSum(_tradeHistory.MPdate, _tradeHistory.YPdate);

            this.CompanyYearSum = ICompanyYearSum.GetCompanyYearSum(_tradeHistory.YPdate);

            List<Models.Sys.Groups> _groups = IGroups.GetGroups();

            List<Models.Day.GroupDayChange> _groupDayChanges = IGroupDayChange.GetCompanyDayChange(_tradeHistory.DPdate, _tradeHistory.MPdate, _tradeHistory.YPdate);

            this.JS = Benefit.Tools.ChartAtManager.ChartManager.CompanyDayBar(_groups, _groupDayChanges);


            DisposeInterface();
        }
        /// <summary>
        /// 初始化接口
        /// </summary>
        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            ICompany = new Service.Sys.ServiceCompany(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IGroupDayChange = new Service.Day.ServiceGroupDayChange(db);
            IGroupWeekSum = new Service.Week.ServiceGroupWeekSum(db);
            ICompanyDayChange = new Service.Day.ServiceCompanyDayChange(db);
            ICompanyFiveDayAvg = new Service.Day.ServiceCompanyFiveDayAvg(db);
            ICompanyAvgMonth = new Service.Month.ServiceCompanyAvgMonth(db);
            ICompanyWeekSum = new Service.Week.ServiceCompanyWeekSum(db);
            IGroups = new Service.Sys.ServiceGroups(db);
            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            ICompany = null;
            ITradeHistory = null;
            IGroupDayChange = null;
            IGroupWeekSum = null;
            ICompanyDayChange = null;
            ICompanyFiveDayAvg = null;
            ICompanyAvgMonth = null;
            ICompanyWeekSum = null;
            IGroups = null;
            #endregion
        }
    }
}
